package top.idcard.agent.biz.core;

import top.idcard.agent.biz.entity.Certification;

import java.util.List;
import java.util.Optional;

/**
 * @author wx
 * @version 1.0
 * @date 2021/2/1 11:16 上午
 */
public class SyncToServer {

    public void main(String[] args) {
        //1.获取index
        //1.1获取服务器index
        IndexConfig serverIndex = getServerIndex();

        //2.从文件读取数据  1.2获取本地最大index
        //2.1读取index文件
        IndexConfig localIndex = getLocalIndex();
        serverIndexHandle(serverIndex.getNativeIndex(), localIndex);
        serverIndexHandle(serverIndex.getHongIndex(), localIndex);
        serverIndexHandle(serverIndex.getForeignIndex(), localIndex);
    }

    private void serverIndexHandle(List<IndexConfig.IndexUnit> serverIndexList, IndexConfig localIndex){
        serverIndexList.stream().parallel().forEach(item->{
            List<IndexConfig.IndexUnit> localList = null;
            switch (item.getType()){
                case Native:
                    localList = localIndex.getNativeIndex();
                    break;
                case Hong:;
                    localList = localIndex.getHongIndex();
                    break;
                case Foreign:
                    localList = localIndex.getForeignIndex();
                    break;
                default:;
            }
            Optional<IndexConfig.IndexUnit> optional = localList.stream().filter(litem->item.getDate().equals(litem.getDate())).findFirst();
            if(optional.isPresent()) {
                if (optional.get().getIndex() > item.getIndex()){
                    //2.2依据index读取数据文件
                    List<Certification> dataList = readUnsyncData(item);
                    //3.同步本地数据到远程服务器
                    syncData(dataList);
                }
            }
        });
    }

    private void syncData(List<Certification> dataList) {

    }

    private List<Certification> readUnsyncData(IndexConfig.IndexUnit item) {
        return null;
    }

    private IndexConfig getLocalIndex() {
        return null;
    }

    //使用netty与服务器通信
    public IndexConfig getServerIndex(){
        return null;
    }
}
